<script setup lang="ts">
import type { FormInstance } from '@arco-design/web-vue'
import { ExamApi } from '@/apis'
import ProFormPanel from '@/components/ProForm/ProFormPanel.vue'
import YearTermCascader from '@/components/ProSelect/YearTermCascader.vue'
import { useForm } from '@/hooks'
import * as Regexp from '@/utils/regexp'
import { Message } from '@arco-design/web-vue'

const emit = defineEmits<{
  (e: 'change'): void
}>()

const visible = ref(false)
const loading = ref(false)
const readonly = ref(true)

const dataId = ref('')

const isCreateMode = computed(() => dataId.value === '')
const title = computed(() => (isCreateMode.value ? '新增考试信息' : '修改考试信息'))

const formRef = ref<FormInstance>()

const { form, resetForm } = useForm({
  name: null,
  examType: null,
  classYear: null,
  term: null,
  grade: null,
  subject: null,
  remark: null,
})

const rules: FormInstance['rules'] = {
  name: [{ required: true, match: Regexp.Name, message: '名称只能含有中文、数字、英文、下划线、短横线,长度为2-32位' }],
}

// 打开新增
const create = async () => {
  dataId.value = ''
  loading.value = false
  visible.value = true
  readonly.value = false
}

// 保存
const onSave = async () => {
  try {
    const isInvalid = await formRef.value?.validate()
    if (isInvalid) {
      return false
    }
    if (isCreateMode.value) {
      await ExamApi.create(form)
      Message.success('添加成功')
    } else {
      await ExamApi.update(form, dataId.value)
      Message.success('修改成功')
    }
    emit('change')
    visible.value = false
  } catch {
    return false
  }
}

// 重置
const reset = () => {
  formRef.value?.resetFields()
  resetForm()
}

defineExpose({ create })
</script>

<template>
  <ProFormPanel v-model:visible="visible" mode="drawer" :title="title" :loading="loading" :footer="isCreateMode"
    @confirm="onSave" @close="reset">
    <a-form ref="formRef" layout="vertical" :model="form" :rules :readonly>
      <a-card title="基础信息" class="section">
        <a-row :gutter="28">
          <readonly-form-item label="名称" field="name">
            <a-input v-model.trim="form.name" placeholder="请输入名称" />
          </readonly-form-item>
          <readonly-form-item label="类型" field="examType">
            <DictSelect v-model="form.examType" placeholder="请选择类型" code="ExamTypeEnum" type="select" />
          </readonly-form-item>
          <readonly-form-item label="年份/学期" field="classYear">
            <YearTermCascader v-model:class-year="form.classYear" v-model:term="form.term" />
          </readonly-form-item>
          <readonly-form-item label="年级" field="grade">
            <DictSelect v-model="form.grade" placeholder="请选择年级" code="Grade" type="select" />
          </readonly-form-item>
          <readonly-form-item label="科目" field="subject">
            <DictSelect v-model="form.subject" placeholder="请选择科目" code="Subject" type="select" />
          </readonly-form-item>
          <readonly-form-item label="备注" field="remark">
            <a-input v-model.trim="form.remark" placeholder="请输入备注" />
          </readonly-form-item>
        </a-row>
      </a-card>
    </a-form>
  </ProFormPanel>
</template>

<style lang="less" scoped></style>
